<?php
namespace app\v1_0\controller\company;

use think\Db;
use think\Exception;

class RecruitmentPlatform extends \app\v1_0\controller\common\Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->checkLogin(1);
        $this->interceptCompanyProfile();
    }

    public function index(){

       $recruitment_platform = Db::table(config('database.prefix') . 'recruitment_platform')
           ->field("id,name as `cate`,url,src")
           ->where('com_id','eq',$this->company_profile->id)
           ->select();
       $total = count($recruitment_platform);
       foreach ($recruitment_platform as $key=>$item){
           $children = Db::table(config('database.prefix') . 'recruitment_platform_auth')
               ->alias('rpa')
               ->field("rpa.*,hr_name as hr,platform_id as p_id")
               ->where(['platform_id'=>$item['id'],'rpa.com_id'=>$this->company_profile->id])
               ->select();

           $childrenCount = count($children);
           $recruitment_platform[$key]['childrenCount'] = $childrenCount;
           if($childrenCount == 0){
               $children[]=[
                   'id'=>($total+999999)+$key,
                   'platform_id'=>'-1',
                   'username'=>'',
                   'hr'=>'',
                   'p_id'=>$item['id'],
               ];
           }else{
               foreach ($children as $k=>$val)
               {
                   $children[$k]['id'] = $total+$val['id'];
                   $children[$k]['tid'] =  $val['id'];
               }
           }

           $children[0]['btn']=true;
           $recruitment_platform[$key]['children'] = $children;
        }
       //获取Hr
        $company_staff_manage = Db::table(config('database.prefix') . 'company_staff_manage')
            ->field("name as `label`")
            ->where(['recruit_team'=>"是",'com_id'=>$this->company_profile->id])
            ->select();
        $company_contact = Db::table(config('database.prefix') . 'company_contact')
            ->field("contact as `label`")
            ->where(['comid'=>$this->company_profile->id])
            ->find();
         array_unshift($company_staff_manage,$company_contact);

        $return['items'] = $recruitment_platform;
        $return['contact'] = $company_contact['label'];
        $return['staff_manage'] = $company_staff_manage;
        $return['recruit_platform'] = $this->recruit_platform;
        $this->ajaxReturn(200, '获取数据成功', $return);
    }
    public function info()
    {
        $id=input('get.id/d', 0, 'intval');
        if(!$id){
            $this->ajaxReturn(200, '缺少参数');
        }
        $result = model('RecruitmentPlatform')->where('id','eq',$id)->find();
        $this->ajaxReturn(200, '获取数据成功', $result);
    }
    public function delBatch($ids = null,$isReturn=true){
        $ids=$ids ? $ids : input('post.id/a');
        if(empty($ids))
        {
           return !$isReturn ? false : $this->ajaxReturn(500, '缺少参数');
        }
        foreach ($ids as $id)
        {
            $this->del($id,false);
        }
        return !$isReturn ? true : $this->ajaxReturn(200, '删除成功');
    }
    public function del($id = null,$isReturn=true)
    {
        $id=$id ? $id : input('post.id/d', 0, 'intval');
        if(!$id){
            return !$isReturn?false: $this->ajaxReturn(500, '缺少参数');
        }
        \think\Db::startTrans();
        try {
            model('RecruitmentPlatformAuth')->where('platform_id', 'in', $id)->delete();
            model('RecruitmentPlatform')->where(['id'=> $id])->delete();

            \think\Db::commit();
        } catch (\Exception $e) {
            \think\Db::rollBack();
            return !$isReturn? false: $this->ajaxReturn(500, $e->getMessage());
        }
        return !$isReturn? true: $this->ajaxReturn(200, '删除成功');

    }

    public function save()
    {
        $id=input('post.id/d', 0, 'intval');
        $input = [
            'com_id' => $this->company_profile->id,
            'name' => input('post.name/s', '', 'trim,badword_filter'),
            'src' => input('post.src/s', '', 'trim,badword_filter'),
            'url' => input('post.url/s', '', 'trim,badword_filter'),
        ];
        $exists = \app\common\model\RecruitmentPlatform::where('com_id',$this->company_profile['id'])
            ->where('name',$input['name'])
            ->count();
        if($exists){
            return $this->returnJson(500,'此招聘平台名称已存在');
        }
        if($id){

            $result = model('RecruitmentPlatform')
                ->allowField(true)
                ->save($input, [
                    'id' => $id
                ]);
        }else{

            $result = model('RecruitmentPlatform')
                ->allowField(true)
                ->save($input);
            if($result){
                $id = model('RecruitmentPlatform')->id;
            }

        }

        $this->ajaxReturn(200, '保存成功', [
            'id' => $id,
            'result' => $result,
        ]);
    }
    public function authSave()
    {
        $id=input('post.id/d', 0, 'intval');
        $input = [
            'com_id' => $this->company_profile->id,
            'username' => input('post.username/s', '', 'trim,badword_filter'),
            'hr_name' => input('post.hr/s', '', 'trim,badword_filter'),
            'platform_id' => input('post.p_id/d', 1, 'intval'),
        ];
        if($id){

            $result = model('RecruitmentPlatformAuth')
                ->allowField(true)
                ->save($input, [
                    'id' => $id
                ]);
        }else{

            $result = model('RecruitmentPlatformAuth')
                ->allowField(true)
                ->save($input);
            if($result){
                $id = model('RecruitmentPlatformAuth')->id;
            }

        }

        $this->ajaxReturn(200, '保存成功', [
            'id' => $id,
            'result' => $result,
        ]);
    }


    public function authInfo()
    {
        $id=input('get.id/d', 0, 'intval');
        if(!$id){
            $this->ajaxReturn(200, '缺少参数');
        }

        $result = model('RecruitmentPlatformAuth')
            ->field('id,username,hr_name as hr,platform_id as p_id')
            ->where('id','eq',$id)
            ->find();
        $this->ajaxReturn(200, '获取数据成功', $result);
    }

    public function authDelBatch($ids = null,$isReturn=true){
        $ids=$ids ? $ids : input('post.id/a');
        if(empty($ids))
        {
            return !$isReturn?false:  $this->ajaxReturn(500, '缺少参数');
        }
        foreach ($ids as $id)
        {
            $this->authDel($id,false);
        }
        return !$isReturn? true:  $this->ajaxReturn(200, '删除成功');
    }
    public function authDel($id = null,$isReturn=true)
    {
        $id=$id ? $id : input('post.id/d', 0, 'intval');
        if(!$id){
            return !$isReturn?false: $this->ajaxReturn(500, '缺少参数');
        }
        \think\Db::startTrans();
        try {
            model('RecruitmentPlatformAuth')->where(['id'=> $id])->delete();

            \think\Db::commit();
        } catch (\Exception $e) {
            \think\Db::rollBack();
            return !$isReturn? false: $this->ajaxReturn(500, $e->getMessage());
        }
        return !$isReturn? true: $this->ajaxReturn(200, '删除成功');

    }

    public function handleDeleteAll()
    {
        $delBatch=input('post.delBatch/a', []);
        $authDelBatch=input('post.authDelBatch/a', []);
        if(empty($delBatch) && empty($authDelBatch)){
            $this->ajaxReturn(500, '缺少参数');
        }
        if(!empty($delBatch)){
            $this->delBatch($delBatch,false);
        }
        if(!empty($authDelBatch)){
            $this->authDelBatch($authDelBatch,false);
        }
        $this->ajaxReturn(200, '删除成功');
    }

}